package com.ruiyou.ub;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
        FragmentActivity activity = getActivity();
        if (activity == null) return;
        Preference exitPref = findPreference(getString(R.string.key_exit_settings));
        if (exitPref != null) {
            exitPref.setOnPreferenceClickListener(preference -> {
                activity.getSupportFragmentManager()
                        .beginTransaction()
                        .remove(SettingsFragment.this)
                        .commit();
                return true;
            });
        }
        Preference restorePref = findPreference(getString(R.string.key_restore_default));
        if (restorePref != null) {
            restorePref.setOnPreferenceClickListener(preference -> {
                Util.Callback callback = (result) -> {
                    PreferenceManager.getDefaultSharedPreferences(activity)
                            .edit().clear().apply();
                    // Update UI
                    activity.getSupportFragmentManager().beginTransaction()
                            .replace(R.id.drawer_layout, new SettingsFragment())
                            .commit();
                };
                Util.showDialog(
                        activity,
                        getString(R.string.restore_default),
                        getString(R.string.confirm_restore),
                        DialogType.CONFIRM,
                        callback
                );
                return true;
            });
        }
        Preference filePermissionPref = findPreference(getString(R.string.key_file_permission));
        if (null != filePermissionPref && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            filePermissionPref.setOnPreferenceClickListener(preference -> {
                Intent it = new Intent(
                        android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                it.setData(android.net.Uri.parse("package:" + activity.getPackageName()));
                startActivity(it);
                return true;
            });
        }
    }

    @NonNull
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        view.setBackgroundResource(R.color.secondary_light_color);
        return view;
    }
}